package interview.parseint;

public class ParseInt {
	public int String2Int(String str) {
		char[] sArray = str.toCharArray();
		boolean minus = (sArray[0] == '-');
		int result = 0;
		int i = minus ? 1 : 0;
		for(; i < sArray.length; i++) {
			result *= 10;
			result += sArray[i] - '0';
		}
		if(minus)
			result *= -1;
		return result;
	}
	
	public String Int2String(int num) {
		StringBuilder sb = new StringBuilder();
		if(num < 0) {
			sb.append('-');
			num *= -1;
		}
		while(num > 0) {
			sb.append(num % 10);
			num /= 10;
		}
		char[] array = sb.toString().toCharArray();
		int i = 0;
		if(array[0] == '-') {
			i = 1;
		}
		char buf;
		for(int j = array.length - 1; i < j; i++, j--) {
			buf = array[i];
			array[i] = array[j];
			array[j] = buf;
		}
		return new String(array);
	}
}
